package com.yupi.springbootinit.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.common.ResultUtils;
import com.yupi.springbootinit.exception.BusinessException;
import com.yupi.springbootinit.model.dto.orders.OrdersUpdateRequest;
import com.yupi.springbootinit.model.entity.Orders;
import com.yupi.springbootinit.model.entity.User;
import com.yupi.springbootinit.service.OrdersService;
import com.yupi.springbootinit.mapper.OrdersMapper;
import com.yupi.springbootinit.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
* @author zjb13
* @description 针对表【orders】的数据库操作Service实现
* @createDate 2024-11-28 22:39:31
*/
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders>
    implements OrdersService{

    @Override
    public Boolean updateOrder(OrdersUpdateRequest ordersUpdateRequest) {
        Orders order = this.getById(ordersUpdateRequest.getOrderId());
        if (order.getOrderStatus()==3){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"正在上课，无法修改");
        }
        //修改上课次数
        Integer oldLessonCount = order.getLessonCount();
        Integer lastLessonCount = order.getLastLessonCount();
        Integer newLessonCount = ordersUpdateRequest.getLessonCount();
        //如果新上课次数少于原上课次数，减少的上课次数大于剩余上课次数，则错误
        if (newLessonCount<oldLessonCount&&lastLessonCount<=(oldLessonCount - newLessonCount)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"剩余课程次数不足,少于课数差");
        }
        //如果新上课次数小于原上课次数，剩余上课次数大于课数差
        else if (newLessonCount<oldLessonCount&&lastLessonCount>(oldLessonCount - newLessonCount)) {
            order.setLastLessonCount(lastLessonCount-(oldLessonCount - newLessonCount));
            order.setLessonCount(newLessonCount);
        }
        //如果新上课次数大于原来上课次数，则修改剩余剩余上课次数和上课次数
        else if (newLessonCount>oldLessonCount&&lastLessonCount>0) {
            Integer count = newLessonCount - oldLessonCount;
            order.setLastLessonCount(lastLessonCount+count);
            order.setLessonCount(newLessonCount);
        }
        //修改金额
        Double oldPrice = order.getLessonPrice();
        if (oldPrice!=ordersUpdateRequest.getLessonPrice()) {
            order.setLessonPrice(ordersUpdateRequest.getLessonPrice());
        }
        //是否修改了线上线下，如果改成线上，要删内容，改成线下要删内容
        Integer oldIsOnline = order.getIsOnline();
        Integer newIsOnline = ordersUpdateRequest.getIsOnline();
        if (oldIsOnline!=newIsOnline) {
            if (newIsOnline==0) {
                String offlineAddress = ordersUpdateRequest.getOfflineAddress();
                order.setOfflineAddress(offlineAddress);
            }else{
                order.setOfflineAddress(null);
            }
            order.setIsOnline(newIsOnline);
        }
        //如果修改了上课科目
        String oldSubject = order.getSubjects();
        String newSubject = ordersUpdateRequest.getSubjects();
        if (!oldSubject.equals(newSubject)) {
            order.setSubjects(newSubject);
        }
        boolean result = this.updateById(order);
        return result;
    }
}




